<template>
  <div class="login_container">
      <div class="login_box" v-loading="loading" element-loading-text="登录中">
        <!-- 头像 -->
        <div class="avatar_box">
          <img src="@/assets/logo.png" alt="">
        </div>
        <!-- 表单 -->
        <el-form ref="loginForm" :model="loginForm" label-width="0" :rules="LoginFormRules" class="login_form">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user" name="username" ref="username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" @keyup.enter.native="login" placeholder="请输入密码" prefix-icon="el-icon-lock" type="password" name="password" ref="password"></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="login" >登录</el-button>
            <el-button type="warning" @click="resetLoginForm('loginForm')">清空</el-button>
          </el-form-item>
        </el-form>  
      </div>
  </div>
</template>

<script>
import { pwdLogin } from '@/api/index';
import crypto from 'crypto'
export default {
    data() {
      return {
        loading:false,
        loginForm:{
          username:'',
          password:''
        },
        LoginFormRules:{
          username:[
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ],
          password:[
            { required: true, message: '请输入密码', trigger: 'blur' },
          ]
        }
      }
    },
    mounted() {
      if (this.loginForm.username === '') {
        this.$refs.username.focus()//自动聚焦
      } else if (this.loginForm.password === '') {
        this.$refs.password.focus()
      }
    },
    methods: {
      // 密码md5加密
      setMd5(){
        var md5 = crypto.createHash("md5")
        md5.update(this.loginForm.password)
        this.loginForm.password = md5.digest('hex')
      },

      // 重置表单 
      resetLoginForm(loginForm) {
        this.$refs[loginForm].resetFields();
      },
      login2(){
        console.log(111)
        this.login()
      },
      // 点击登录
      login() {
        const _this = this
        _this.setMd5()
        _this.$refs['loginForm'].validate(async (valid) => {
          if (valid) {
            _this.loading = true
            const res = await pwdLogin(this.loginForm)
            if (res.code == 200){
              _this.$message({
                message:'登录成功',
                type:'success'
              })
              var user = res.data.user[0]
              var user_str = JSON.stringify(user)
              window.localStorage.setItem('user',user_str)
              window.localStorage.setItem('token',res.data.token)
              _this.$router.push('/index/user')
            }
          } else {
            return false
          }
          // 延迟解除loading 防止点击过快
          setTimeout(function(){
            _this.loading = false
          },1000)
        })
      }
    }
}
</script>

<style lang="less" scoped>
.login_container{
  background-color: #000;
  height: 100%;
}

.login_box{
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  box-shadow: 0 0 10px #ddd;
  transform: translate(-50%,-50%)
  
}

.avatar_box{
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 100px #ddd;
  position: absolute;
  left:50%;
  transform: translate(-50%,-50%);
  background-color: #fff;
  img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}

.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}

.btns{
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
</style>>

